package com.workcell.workdata.xthings.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.common.excel.annotation.ResponseExcel;
import com.workcell.workdata.xthings.dto.BaseVo;
import com.workcell.workdata.xthings.entity.BaseTvBox;
import com.workcell.workdata.xthings.entity.Manufacturer;
import com.workcell.workdata.xthings.service.BaseTvBoxService;
import com.workcell.workdata.xthings.service.ManufacturerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 电视盒子型号
 *
 * @author guogyu
 * @date 2023-03-01 10:15:50
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basetvbox" )
@Api(value = "basetvbox", tags = "电视盒子型号管理")
public class BaseTvBoxController {
    private final BaseTvBoxService baseTvBoxService;
    private final ManufacturerService manufacturerService;

	/**
	 * 模糊分页查询
	 * @param baseVo
	 * @return
	 */
	@ApiOperation(value = "模拟分页查询", notes = "模拟分页查询")
	@PostMapping("/vaguePage" )
	public R vaguePage(@RequestBody BaseVo baseVo) {
		return R.ok(baseTvBoxService.vaguePage(baseVo));
	}

    /**
     * 通过id查询
     * @param  id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(baseTvBoxService.getById(id));
    }

    /**
     * 新增
     * @param baseTvBox
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping
    public R save(@RequestBody BaseTvBox baseTvBox) {
        Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseTvBox.getDeviceManufacturer());
        Assert.notNull(manufacturer, "厂商查询为空");
        baseTvBox.setManufacturerShortName(manufacturer.getManufacturerShortName());
        baseTvBox.setManufacturerFullName(manufacturer.getManufacturerFullName());
        baseTvBox.setManufacturer(manufacturer.getManufacturerFullName());
		baseTvBox.setManufacturerCode(manufacturer.getManufacturerCode());
        return R.ok(baseTvBoxService.save(baseTvBox));
    }

    /**
     * 修改电视盒子型号基本信息
     * @param baseTvBox
     * @return R
     */
    @ApiOperation(value = "修改电视盒子型号基本信息", notes = "修改电视盒子型号基本信息")
    @PutMapping
    public R updateBaseTvBox(@RequestBody BaseTvBox baseTvBox) {
		Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseTvBox.getDeviceManufacturer());
		Assert.notNull(manufacturer, "厂商查询为空");
		baseTvBox.setManufacturerShortName(manufacturer.getManufacturerShortName());
		baseTvBox.setManufacturerFullName(manufacturer.getManufacturerFullName());
		baseTvBox.setManufacturer(manufacturer.getManufacturerFullName());
		baseTvBox.setManufacturerCode(manufacturer.getManufacturerCode());
        return R.ok(baseTvBoxService.updateBaseTvBox(baseTvBox));
    }

	/**
	 * 修改状态
	 * @param baseTvBox
	 * @return R
	 */
	@ApiOperation(value = "修改状态", notes = "修改状态")
	@PutMapping("/updateStateById")
	public R updateStateById(@RequestBody BaseTvBox baseTvBox) {
		return R.ok(baseTvBoxService.updateStateById(baseTvBox));
	}

    /**
     * 通过id删除
     * @param id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @DeleteMapping("/{id}" )
    public R delById(@PathVariable Long id) {
        return R.ok(baseTvBoxService.delById(id));
    }

    /**
     * 导出excel 表格
     * @param baseTvBox 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<BaseTvBox> export(BaseTvBox baseTvBox) {
        return baseTvBoxService.list(Wrappers.query(baseTvBox));
    }
}
